package com.ideal.frame.util;

import com.ideal.frame.filter.RequestWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.util.ContentCachingRequestWrapper;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public final class NetworkUtil {

    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (checkIp(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (checkIp(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (checkIp(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (checkIp(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (checkIp(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip != null && ip.length() > 15) { // "***.***.***.***".length() == 15
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            ip = getHostAddress();
        }
        return ip;
    }

    private static boolean checkIp(String ip){
        return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip);
    }

    private static String getHostAddress() {
        // 根据网卡取本机配置的IP
        InetAddress inet = null;
        try {
            inet = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return inet.getHostAddress();
    }

    public static String getParams(HttpServletRequest request){
        if (request.getMethod().equals("GET")
                || request.getMethod().equals("DELETE")
                || StringUtils.equals(request.getContentType(),"application/x-www-form-urlencoded")) {
            return request.getParameterMap().toString();
        } else {
            if(request instanceof RequestWrapper)
                return new String(((RequestWrapper)request).getBody());
        }
        log.warn("没有拿到参数");
        return null;
    }

    public static String getUserAgent(HttpServletRequest request){
        String userAgent = null;
        String header = request.getHeader("user-agent");
        if (StringUtils.isNotEmpty(header)) {
            if (header.indexOf("MSIE") > 0) {
                userAgent = "IE";
            } else if (header.indexOf("Firefox") > 0) {
                userAgent = "Firefox";
            } else if (header.indexOf("Chrome") > 0) {
                userAgent = "Chrome";
            } else if (header.indexOf("Safari") > 0) {
                userAgent = "Safari";
            } else if (header.indexOf("Camino") > 0) {
                userAgent = "Camino";
            } else if (header.indexOf("Konqueror") > 0) {
                userAgent = "Konqueror";
            }
        } else {
            log.warn("Can not find http header!");
        }
        return userAgent;
    }
}
